//
// Created by ChenJi on 2021/7/30.
//

#include <usart.h>
#include "UartController.h"

uint8_t uart1_rx_buffer[256];
uint8_t uart2_rx_buffer[256];

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;

void HC_UartController_Init() {
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1, uart1_rx_buffer, 256);
    HAL_UARTEx_ReceiveToIdle_DMA(&huart2, uart2_rx_buffer, 256);
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
    if (huart == &huart1) {
        onUart1Message(uart1_rx_buffer, Size);
        HAL_UARTEx_ReceiveToIdle_DMA(&huart1, uart1_rx_buffer, 256);
    }
    if (huart == &huart2) {
        onUart2Message(uart2_rx_buffer, Size);
        HAL_UARTEx_ReceiveToIdle_DMA(&huart2, uart2_rx_buffer, 256);
    }


}

void sendMessageUart1(uint8_t *data, uint8_t size) {
    HAL_UART_Transmit(&huart1, data, size, 0xFFFF);
}
void sendMessageUart2(uint8_t *data, uint8_t size) {
    HAL_UART_Transmit(&huart2, data, size, 0xFFFF);
}

void sendMessageUart2_DMA(uint8_t *data, uint8_t size) {
    HAL_UART_Transmit_DMA(&huart2, data, size);
}
void sendMessageUart1_DMA(uint8_t *data, uint8_t size) {
    HAL_UART_Transmit_DMA(&huart1, data, size);
}

